package ru.ognick.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;

import ru.ognick.logic.Praxis;
import ru.ognick.logic.Training;
import ru.ognick.logic.Praxis.PraxisType;

import android.app.Activity;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class FileManager {
    public static final String TAG = "FileManager";
    public final Activity activity;
    
    public FileManager(Activity activity) {
    	this.activity = activity;
	}
    
    public File getRoot() {
    	return activity.getFilesDir();
    }
    
    public File[] getFiles() {
	    return getRoot().listFiles(new FilenameFilter() {
		
			public boolean accept(File dir, String filename) {
				return filename.endsWith(".js");
			}
		});
    }
    
    public static String asString(File file) throws IOException, JSONException {
		FileInputStream fis = new FileInputStream(file);
		BufferedInputStream bis = new BufferedInputStream(fis);
		byte buffer[] = new byte[(int)file.length()];
		bis.read(buffer,0,buffer.length);  
		fis.close();
		return new String(buffer);

	}
    
    public static Training loadTraining(File file) throws IOException, JSONException {
		JSONArray j = new JSONArray(asString(file));
        Gson gson = new GsonBuilder().create();
 		return gson.fromJson(j.toString(), Training.class);
    }
    
   
    public boolean delete (File file) {
    	return file.delete();
    }
    
    public boolean touch(String filename) {
		try {
			File file = new File (getRoot(),filename);
			file.createNewFile();
			save_def(filename);
			return true;
		} catch (IOException e) {
			Log.e(TAG,e.getMessage());
			return false;
		}
    }
    
	public void save_def(String filename) {
		File file = new File(getRoot(),filename);
		Training training = new Training();
		String contents[] = {"Пресс","Подтягивания","Прыжки","Отжимания","Лягушечка","Пресс","Подтягивания","Прыжки","Отжимания","Лягушечка"};
        for (int i=0; i < 5; i++)
        	training.add(new Praxis(PraxisType.action, contents[i], "Расширенное описание : " + contents[i], i,2,5, null));
        
//        training.add(new Praxis(PraxisType.passive, contents[0], "----------------------------------------------------------------------------- -------------------------- -------------------- Расширенное описание : " + contents[0], 10,5,5, null));

        GsonBuilder gsonb = new GsonBuilder();
        Gson gson = gsonb.create();
        String jString = gson.toJson(training); 
        FileOutputStream fos;
        try {
  			file.createNewFile();
        	fos = new FileOutputStream(file);
        	fos.write(jString.getBytes());
        	fos.close();
        	Log.d (TAG, "Save complite " + file.getPath());
        } catch (IOException e) {
        	Log.e(TAG,e.getMessage());
        }
        training = null;
	}
	
	public static void save(File file, Training training) {
        GsonBuilder gsonb = new GsonBuilder();
        Gson gson = gsonb.create();
        String jString = gson.toJson(training); 
        FileOutputStream fos;
        try {
  			file.createNewFile();
        	fos = new FileOutputStream(file);
        	fos.write(jString.getBytes());
        	fos.close();
        	Log.d (TAG, "Save complite " + file.getPath());
        } catch (IOException e) {
        	Log.e(TAG,e.getMessage());
        }
        training = null;
	}

}
